#!/usr/bin/python
# coding: utf-8
import sys
import Image
from itools import parser, test 

if __name__ == '__main__':
        outf = '/tmp/result.pgm'

	argvs = sys.argv
        argc  = len(argvs)
        if ( argc != 2 ):
                print "Usage: tool <FileName>"
                sys.exit(1)
		
	im = Image.open(argvs[1])
        nim = im.copy()
        (width, height) = im.size
        nim = nim.convert("1")
	pix = nim.load()
	
#行合計
	list = parser.get_horizontal_sum(pix, width, height)

#最適なWIDTH,閾値
	(opt_width, opt_threshold) = parser.get_optimal_threshold_width(list)	
	print (opt_width, opt_threshold)
#ブロック
	blocks = parser.get_horizontal_blocks(list, opt_width, opt_threshold)

	test.write_image_with_splitline(im, width, height, blocks, outf)
	print blocks
